08. 1D Vector Playground
Playground
Now it's your turn to make some programs with vectors. Here is a playground where you can write your program.
Idea 1
First, try writing a program that initializes a vector of size 3. The values for this vector are [5, 10, 27]. Initialize another vector of size 3 with the values [3, 17, 12]. Now subtract the two vectors from each other and output the results.
To get even more practice, write a function that takes in two vectors and then outputs the difference between the vectors. Assume that the two vectors are the same size; otherwise you would have to check that they are the same size and include some error checking.
Idea 2
Initialize a vector with the values [17, 10, 31, 5, 7]. Initialize another vector with the values [3, 1, 6, 19, 8]. Then, output another vector that contains the product of each element. In other words, the vector should have [17\times3, \space10\times1, \space 31\times6, \space 5\times19, \space 7\times8].
To get even more practice, write a function that takes in two vectors and then outputs a new vector that is the result of element by element multiplication. Assume that the two vectors are the same size; otherwise you would have to check that they are the same size and do some error checking.
Start Quiz:
//TODO: Use this as a playground to practice with vectors
//TODO:
// Fill out your program's header. The header should contain any necessary
// include statements and also function declarations
//TODO:
// Write your main program. Remember that all C++ programs need
// a main function. The most important part of your program goes
// inside the main function.
#include <iostream>
#include <vector>
using namespace std;
// function declaration
vector<float> vectorsubtraction(vector<float> vector1, vector<float> vector2);
// program that computes the difference between two vectors
int main() {
// declare and initialize vectors
vector<float> v1(3);
vector<float> v2(3);
v1[0] = 5.0;
v1[1] = 10.0;
v1[2] = 27.0;
v2[0] = 2.0;
v2[1] = 17.0;
v2[2] = 12.0;
vector<float> v3 (v1.size());
// calculate the difference between the two vectors
v3 = vectorsubtraction(v1, v2);
// print out the results of the vector subtraction
for (int i = 0; i < v3.size(); i++) {
cout << v3[i] << " ";
}
cout << endl;
return 0;
}
// define the function -
// INPUTS: two vectors
// OUTPUT: the difference between the two vectors
vector<float> vectorsubtraction(vector<float> vector1, vector<float> vector2) {
vector<float> vectordifference (vector1.size());
for (int i = 0; i < vector1.size(); i++) {
vectordifference[i] = vector1[i] - vector2[i];
}
return vectordifference;
}
#include <iostream>
#include <vector>
using namespace std;
// function declaration
vector<float> vectormultiply(vector<float> vector1, vector<float> vector2);
// program that computes the element-wise multiplication of two vectors
int main() {
// declare and initialize vectors
vector<float> v1(5);
vector<float> v2(5);
v1[0] = 17.0;
v1[1] = 10.0;
v1[2] = 31.0;
v1[3] = 5.0;
v1[4] = 7.0;
v2[0] = 3.0;
v2[1] = 1.0;
v2[2] = 6.0;
v2[3] = 19.0;
v2[4] = 8.0;
vector<float> v3 (v1.size());
// calculate the difference between the two vectors
v3 = vectormultiply(v1, v2);
// print out the results of the vector multiplication
for (int i = 0; i < v3.size(); i++) {
cout << v3[i] << " ";
}
cout << endl;
return 0;
}
// define the function -
// INPUTS: two vectors
// OUTPUT: multiplies elements together into a new vector
vector<float> vectormultiply(vector<float> vector1, vector<float> vector2) {
vector<float> vectorproduct (vector1.size());
for (int i = 0; i < vector1.size(); i++) {
vectorproduct[i] = vector1[i] * vector2[i];
}
return vectorproduct;
}